/*
 * @Author: zhangyujie
 * @Date:   2015-08-12 10:45:39
 * @Last Modified by:   zhangyujie
 * @Last Modified time: 2015-08-14 13:56:00
 */


define(['jquery', 'd3', 'util','hammer', 'util'], function($, d3, util,Hammer) {
	function smallSingleRing(selector) {
		var setting = {
                title:{
                    text:'参与人员',
                    subtext:'投资分布',
                    textStyle:{
                        'font-size':'20px',
                        'fill':'#8C91A0',
                        'font-weight':'bold',
                        'text-anchor':'middle',
                        'font-famliy':'微软雅黑'
                    },
                    subtextStyle:{
                        'font-size':'20px',
                        'fill':'#8C91A0',
                        'font-weight':'bold',
                        'text-anchor':'middle',
                        'font-famliy':'微软雅黑'

                    }

                },
                selector: selector,
                radius: ['20%', '23%'],
                series: [{
                    'name': '产品XXA',
                    'value': 20
                }, {
                    'name': '产品XXB',
                    'value': 10
                }, {
                    'name': '产品XXC',
                    'value': 10
                }, {
                    'name': '产品XXD',
                    'value': 20
                }, ],
                colorset: ['#E9B8FD', '#FDAC95', '#95F3BC', '#8CE9FD'],
                margin: {
                    top: 10,
                    right: 10,
                    left: 20,
                    bottom: 10
                },
                nameStyle: {
                    fontSize: '12px',
                    fontWeight: '600',
                    color: '#818181'
                },
                valueStyle: {
                    fontSize: '20px',
                    fontWeight: '600',
                    color: '#818181'
                },
                // 圆环出的斜线
                diffDistance:{
                    length:30,
                    stroke:'',
                    strokeWidth:'1'
                },
                // 细横线
                lightLine: {
                    length:40,
                    strokeWidth:'1'
                },
                // 粗横线
                boldLine: {
                    length:35,
                    strokeWidth:'2'
                }
            }

        seven(setting)
		function seven(setting) {

			var selector = setting.selector,
				series = setting.series,
				colorset = setting.colorset,
				margin = setting.margin,
				nameStyle = setting.nameStyle,
				valueStyle = setting.valueStyle,
				width = document.querySelector(selector).offsetWidth,
				height = document.querySelector(selector).offsetHeight,
				innerRadius = util.util.getNumByRate(width, setting.radius[0]),
				outerRadius = util.util.getNumByRate(width, setting.radius[1]),
				startAngle = 0,
				endAngle = 2 * Math.PI,
				arcArray = [],
				lineArray = [],
				lineArrayLight = [],
				lineArrayBold = [],
				sum = 0,
				diffDistance = setting.diffDistance.length,
				lightLine = setting.lightLine.length,
				boldLine = setting.boldLine.length,

				title = setting.title,
				text = title.text,
				subtext = title.subtext,
				textStyle = title.textStyle,
				subtextStyle = title.subtextStyle,
				textRect = util.util.getTextSize(text, textStyle),
				subtextRect = util.util.getTextSize(text, subtextStyle)


			for (var i = 0; i < series.length; i++) {

				sum += series[i].value
			}

			var svg = d3.select(selector)
				.append('svg')
				.attr('width', width)
				.attr('height', height)

			var pie = d3.layout.pie()
				.sort(null)
				.startAngle(startAngle)
				.endAngle(endAngle)
				.value(function(d) {

					return parseFloat(d.value)
				})

			var con = svg.append('g')
				.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')')

			var line = con.append('g'),
				lineLight = con.append('g'),
				lineBold = con.append('g'),
				textValue = con.append('g'),
				textName = con.append('g')

			var arc = d3.svg.arc()
				.innerRadius(innerRadius)
				// .outerRadius(outerRadius)
				.outerRadius(function(d) {

					var value = parseFloat(d.data.value)
					d.r = (outerRadius + innerRadius) / 2

					arcArray.push(d)

					return outerRadius //innerRadius + (radius) * (value / 100.0)
				})



			var path = con.selectAll('.solidArc')
				.data(pie(series))
				.enter().append('path')
				.attr('fill', function(d, i) {

					return colorset[i]
				})
				.attr('class', 'solidArc')
				.attr('d', arc)

			// 添加辅助线
			for (var i = 0, len = arcArray.length; i < len; i++) {

				var x, y, diffAngle, x1, y1, x2, x3

				diffDistance = diffDistance

				diffAngle = (arcArray[i].startAngle + arcArray[i].endAngle) / 2

				x = Math.cos(diffAngle - Math.PI / 2) * (arcArray[i].r)

				y = Math.sin(diffAngle - Math.PI / 2) * (arcArray[i].r)

				x1 = Math.cos(diffAngle - Math.PI / 2) * (arcArray[i].r + diffDistance)

				y1 = Math.sin(diffAngle - Math.PI / 2) * (arcArray[i].r + diffDistance)

				if (x1 > 0) {

					x2 = x1 + lightLine
					x3 = x2 + boldLine
				} else {

					x2 = x1 - lightLine
					x3 = x2 - boldLine
				}

				var temp = {
					x: x,
					y: y,
					x1: x1,
					y1: y1,
					color: colorset[i]
				}

				var parm = {
					x: x1,
					y: y1,
					x1: x2,
					y1: y1,
					color: colorset[i],
					name: arcArray[i].data.name,
					value: ((arcArray[i].data.value * 100 / sum).toFixed(0))

				}

				var parm1 = {
					x: x2,
					y: y1,
					x1: x3,
					y1: y1,
					color: colorset[i],
					name: arcArray[i].data.name,
					value: ((arcArray[i].data.value * 100 / sum).toFixed(0))
				}

				lineArray.push(temp)

				lineArrayLight.push(parm)

				lineArrayBold.push(parm1)
			}

			con.selectAll('.line')
				.data(lineArray)
				.enter()
				.append('line')
				.attr('class', 'line')
				.attr('x1', function(d, i) {

					return d.x
				})
				.attr('y1', function(d, i) {

					return d.y
				})
				.attr('x2', function(d, i) {

					return d.x1
				})
				.attr('y2', function(d, i) {

					return d.y1
				})
				.attr('stroke', function(d, i) {

					return d.color
				})
				.style('stroke-width', '1')

			lineLight.selectAll('.lightLine')
				.data(lineArrayLight)
				.enter()
				.append('line')
				.attr('class', 'lightLine')
				.attr('x1', function(d, i) {

					return d.x
				})
				.attr('y1', function(d, i) {

					return d.y
				})
				.attr('x2', function(d, i) {

					return d.x1
				})
				.attr('y2', function(d, i) {

					return d.y1
				})
				.attr('stroke', function(d, i) {

					return d.color
				})
				.attr('stroke-width', setting.lightLine.strokeWidth)

			lineBold.selectAll('.boldLine')
				.data(lineArrayBold)
				.enter()
				.append('line')
				.attr('class', 'boldLine')
				.attr('x1', function(d, i) {

					return d.x
				})
				.attr('y1', function(d, i) {

					return d.y
				})
				.attr('x2', function(d, i) {

					return d.x1
				})
				.attr('y2', function(d, i) {

					return d.y1
				})
				.attr('stroke', function(d, i) {

					return d.color
				})
				.attr('stroke-width', setting.boldLine.strokeWidth)

			textName.selectAll('.textName')
				.data(lineArrayBold)
				.enter()
				.append('text')
				.attr('class', 'textName')
				.attr('x', function(d, i) {

					if (d.x > 0) {

						return d.x
					} else {

						return d.x1
					}
				})

			.attr('y', function(d, i) {

					return d.y
				})
				.attr('fill', function(d, i) {

					if (d.x < 0) {

						return nameStyle.color
					} else {

						return d.color
					}
				})
				.attr('dx', function(d, i) {

					if (d.x < 0) {

						return '0.1em'
					} else {

						return '0.5em'
					}

				})
				.attr('dy', '-0.15em')
				.text(function(d, i) {

					if (d.x < 0) {

						return d.name
					} else {

						return d.value + '%'
					}

				})
				.style('font-size', function(d, i) {

					if (d.x < 0) {

						return nameStyle.fontSize
					} else {

						return valueStyle.fontSize
					}

				})
				.style('font-weight', valueStyle.fontWeight)

			textValue.selectAll('.textValue')
				.data(lineArrayLight)
				.enter()
				.append('text')
				.attr('class', 'textValue')
				.attr('x', function(d, i) {

					if (d.x > 0) {

						return d.x
					} else {

						return d.x1
					}
				})
				.attr('y', function(d, i) {

					return d.y
				})
				.attr('fill', function(d, i) {

					if (d.x > 0) {

						return valueStyle.color
					} else {

						return d.color
					}
				})
				.attr('dx', '0.35em')
				.attr('dy', '-0.15em')
				.text(function(d, i) {

					if (d.x < 0) {

						return d.value + '%'
					} else {

						return d.name
					}

				})
				.style('font-size', function(d, i) {

					if (d.x < 0) {

						return valueStyle.fontSize
					} else {

						return nameStyle.fontSize
					}

				})
				.style('font-weight', valueStyle.fontWeight)

			con.append('text')
				.text(text)
				.attr('x', 0)
				.attr('y', -(textRect.height * 1 / 2))
				.style(textStyle)
			con.append('text')
				.text(subtext)
				.attr('x', 0)
				.attr('y', subtextRect.height)
				.style(textStyle)

		}
	}

	return {
        smallSingleRing : smallSingleRing
    }
})